/**
 * ImageSelect (v. 0.1 - 2007-07-12 )
 * Copyright (c) 2007 Benjamin Haynor
 * 
 * Inspired by David Spurr's 'JavaScript Image Cropper UI, using Prototype & script.aculo.us' found at http://www.defusion.org.uk/code/javascript-image-cropper-ui-using-prototype-scriptaculous/
 * ImageSelect provides a way to overlay a crop area on an image and capture its coordinates.
 *
 * All rights reserved.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
var ImageSelect = new Class({
    Implements: Events,
    Implements: Options,
    options: {
        onEndResize: function(){},
        startCoords: [0, 0],
        startWidthHeight: [100, 100],
        displayOnInit: false

    },
    
    initialize: function(el, id ,wrapper, drag, options){
        this.setOptions(options);
        this.img = $(el);   //el es la imagen
        
        c = this.img.getCoordinates();
        this.dragArea=drag;
        this.elementId=id;
        this.wrapper=wrapper;
        this.img.hOffset = c.left;
        this.img.vOffset = c.top;
        this.imgW = this.img.width;
        this.imgH = this.img.height;
        
        this.buildTip();
        
        this.drawTip();
        this.setDrags();
    },
    
    setSelAreaXYWH: function(xy, wh){
        x = xy[0]; y = xy[1]; w = wh[0]; h = wh[1];
        x > this.imgW ? x = 0 : x = x;
        y > this.imgH ? y = 0 : y = y;
        x + w > this.imgW ? w = this.imgW - x : w = w;
        y + h > this.imgH ? h = this.imgH - y : h = h;
        $(this.selArea).setStyles({left: x, top: y, width: w, height: h});
    },
    
    drawTip: function(){
        this.getCoords();
        
        if(!this.selArea)
            return;
        this.setSelAreaXYWH(this.options.startCoords, this.options.startWidthHeight);
        
    },
    
    setWrapper: function(e){
        this.wrapper;
    },
    
    getCoords: function(){
        this.imgCoords = this.img.getCoordinates();
        this.coords = this.selArea.getCoordinates();
        this.dragCoords = this.dragArea.getCoordinates();
    },
    
    buildTip: function(){
        
        if( !this.wrapper)
            return;
        
        prefix = "imgCrop_";
        
        this.selArea = new Element("div", {id: prefix + String(this.elementId), 'class': prefix + 'selArea'});
        this.selArea.injectInside(this.dragArea);
        
        this.clickArea = new Element("div", {'class': prefix + 'clickArea'});
        this.clickArea.injectInside(this.selArea);
        
        this.dragger = new Element("div" , {'class':prefix + 'handle '+ prefix + 'handleSE'} );
        this.dragger.injectInside(this.selArea);
    },

    removeOverlay: function(){
        this.img.injectAfter(this.wrapper);
        this.wrapper.remove();
        this.dragArea.remove();

    },
    
    resizeSelArea: function(handle){
        handleCoords = this.handleNW.getCoordinates();
        c=this.img.getCoordinates();
        
        this.selArea.setStyle('width', c.left - handleCoords.left + 5);
        this.selArea.setStyle('height', c.top - handleCoords.top - 3);
    },
    dragAll: function(){
        c = this.dragArea.getCoordinates();
        this.selArea.setStyle('left', c.left);
        this.selArea.setStyle('top', c.top);
    },
    setDrags: function(){
        // NORTH DRAG
        
        var mydrag= new Drag(this.selArea , {
                snap: 1,
                handle:this.clickArea,
                modifiers: {x: 'left', y: 'top'},
                limit: { 
                        x: [0, (this.imgCoords.width.toInt() - this.coords.width.toInt()) ],
                        y: [0, (this.imgCoords.height.toInt() - this.coords.height.toInt()) ]
                        },
                //onDrag: function(){ this.dragAll().bind(this); },
                onComplete : function(){ this.options.onEndResize(this.coords); }.bind(this),
                onDrag: function(){ this.options.onEndResize(this.coords); }.bind(this)
                });
        
        // ONE WAY RE-SIZE
        var enlarge= new Drag( this.selArea, {
                snap:1,
                modifiers: {'x': 'width', y: 'height'},
                handle: this.dragger,
                limit: {
                        x: [10, function(){return this.imgW - this.selArea.style.left.toInt();}.bind(this)],
                        y: [10, function(){return this.imgH - this.selArea.style.top.toInt();}.bind(this)]
                        },
                onComplete: function(){ this.options.onEndResize(this.coords); }.bind(this),
                onDrag: function(){ this.options.onEndResize(this.coords); }.bind(this)
                });
         
    }
});
//ImageSelect.implement(new Events, new Options);
